package com.foreveross.mofang.datapersistence.core;

import java.util.Date;
import java.util.List;

import javax.persistence.Entity;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;


/**
 * 航空采集平台关系性数据库持久化类
 * 
 * @author fb
 */
@Entity
@Table(name="CRAWL")
@NamedQueries({ @NamedQuery(name = "getCrawByRowKey", query = "select ce from CrawlEntity ce where ce.rowKey = :rowKey"),
				@NamedQuery(name = "removeByRowKey", query = "delete from CrawlEntity ce where ce.rowKey in(:rowKeys)")})
public class CrawlEntity extends RDBMSBaseEntity {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	public CrawlEntity(){
		
	}
			
	public CrawlEntity(String rowKey, String content) {
		super(rowKey, content, new Date());
	}

	/**
	 * 根据rowKey获取数据
	 * 
	 * @param rowKey
	 * @return
	 */
	public static CrawlEntity getCraw(String rowKey) {
		 List<CrawlEntity> crawlEntity = getRepository().createNamedQuery("getCrawByRowKey")
				.addParameter("rowKey", rowKey).list();
		 
		 if (crawlEntity == null || crawlEntity.isEmpty()) {
			 return null;
		 }
		
		return crawlEntity.get(0);
	}
	
	/**
	 * 按rowKey批量删除
	 * 
	 * @param rowKeys
	 */
	public static void removes(String[] rowKeys) {
		getRepository().createNamedQuery("removeByRowKey").addParameter("rowKeys", rowKeys).executeUpdate();
	}
}
